package de.akabahn.ebd.comm.dbd;

/**
 * The transfer parameters that are used to communicate with the DBD
 * 
 * @author Peter Eimann
 * @since 0.2
 * @version 0.2
 */
public class DBDParameter {
	private int value;
	private DBDCommand command;
	private String name;

	/**
	 * Default constructor
	 */
	public DBDParameter() {
	}

	/**
	 * Set the transfer parameters that are sent to the DBD. This method should
	 * be used for all commands where no value is sent to the DBD.
	 * 
	 * @param command
	 *            The command
	 * @param name
	 *            The name of the variable (the length must not exceed 16
	 *            characters), converted to upper case
	 * @throws IllegalArgumentException
	 *             Thrown if the input parameters are invalid
	 */
	public DBDParameter(DBDCommand command, String name) {
		this(command, name, 0);
	}

	/**
	 * Set the transfer parameters that are sent to the DBD. This method should
	 * be used for all commands where a value is sent to the DBD.
	 * 
	 * @param command
	 *            The command
	 * @param name
	 *            The name of the variable (the length must not exceed 16
	 *            characters), converted to upper case
	 * @param value
	 *            The value to set (must be >= 0)
	 * @throws IllegalArgumentException
	 *             Thrown if the input parameters are invalid
	 */
	public DBDParameter(DBDCommand command, String name, int value) {
		if (name == null || "".equals(name.trim())) {
			throw new IllegalArgumentException("name must not be null or empty");
		} else if (name.trim().length() > 16) {
			throw new IllegalArgumentException("name is too long (> 16 chars)");
		}
		if (value < 0) {
			throw new IllegalArgumentException("value must be >= 0");
		}

		this.command = command;
		this.name = name.toUpperCase();
		this.value = value;
	}

	/**
	 * @param value
	 *            The value to set (must be >= 0)
	 */
	public void setValue(int value) {
		if (value < 0) {
			throw new IllegalArgumentException("value must be >= 0");
		}
		this.value = value;
	}

	/**
	 * @param command
	 *            the command to set
	 */
	public void setCommand(DBDCommand command) {
		this.command = command;
	}

	/**
	 * @param name
	 *            The name of the variable (the length must not exceed 16
	 *            characters), converted to upper case
	 */
	public void setName(String name) {
		if (name == null || "".equals(name.trim())) {
			throw new IllegalArgumentException("name must not be null or empty");
		} else if (name.trim().length() > 16) {
			throw new IllegalArgumentException("name is too long (> 16 chars)");
		}
		this.name = name.toUpperCase();
	}

	/**
	 * Return the value of the variable
	 * 
	 * @return the value
	 */
	public int getValue() {
		return value;
	}

	/**
	 * Return the DBD command
	 * 
	 * @return the command
	 */
	public DBDCommand getCommand() {
		return command;
	}

	/**
	 * Return the name of the variable
	 * 
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Return the id of the variable (the name except the last character, e.g.
	 * <code>31W10I</code> will return <code>31W10</code>)
	 * 
	 * @return the id
	 */
	public String getId() {
		return (name != null) ? name.substring(0, name.length() - 1) : null;
	}

	/**
	 * Return a string representation of the parameter
	 */
	public String toString() {
		return "DBDParameter [" + command + " | " + name + " | " + value + "]";
	}
}
